#include <stdio.h>
#include <string.h>
#include "ConsoleManager.h"
#include "UnitTest++.h"

char** createCommand(int size)
{
    char** commands = new char*[size];

    for(int i = 0; i < size; i++)
    {
        commands[i] = new char[50];
    }

    return commands;
}

void freeCommand(char** command, int size)
{
    for(int i = 0; i < size; i++)
    {
        delete[] command[i];
    }

    delete[] command;
}

std::string getHelpMessage()
{
    std::string helpMessage = HELP_MS;
    helpMessage += COMPRESS_MS;
    helpMessage += DECOMPRESS_MS;
    helpMessage += LIST_MS;

    return helpMessage;
}

TEST(testHelpCommand)
{
    int commandSize = 2;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-h");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL(getHelpMessage(), resp.message_);
    CHECK_EQUAL(1, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testNavigateCommand)
{
    int commandSize = 3;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-l");
    strcpy(command[2], "/test.zip");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL("Navigate", resp.message_);
    CHECK_EQUAL(0, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testCompressCommand)
{
    int commandSize = 5;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-c");
    strcpy(command[2], "test.zip");
    strcpy(command[3], "/Documents");
    strcpy(command[4], "/test.txt");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL("Compress", resp.message_);
    CHECK_EQUAL(0, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testDecompressCommand)
{
    int commandSize = 4;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-d");
    strcpy(command[2], "resources/");
    strcpy(command[3], "resources/decompressTest.zip");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL("Decompress", resp.message_);
    CHECK_EQUAL(0, resp.code_);
    freeCommand(command, commandSize);
    unlink("resources/My life");
}

TEST(testEmptyArgunments)
{
    int commandSize = 1;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL(getHelpMessage(), resp.message_);
    CHECK_EQUAL(1, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testOthersArgunments)
{
    int commandSize = 2;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-zip");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL(getHelpMessage(), resp.message_);
    CHECK_EQUAL(1, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testManyArgunments)
{
    int commandSize = 5;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-zip");
    strcpy(command[2], "issue");
    strcpy(command[3], "push");
    strcpy(command[4], "hg");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL(getHelpMessage(), resp.message_);
    CHECK_EQUAL(1, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testDecompressCommandWithOnlyOutputArgument)
{
    int commandSize = 3;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-d");
    strcpy(command[2], "resources/");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    std::string expected = NOT_ARG_MS;
    expected += DECOMPRESS_MS;
    CHECK_EQUAL(expected, resp.message_);
    CHECK_EQUAL(1, resp.code_);
    freeCommand(command, commandSize);
}

TEST(testCompressCommandWithManyInputs)
{
    int commandSize = 6;
    char** command = createCommand(commandSize);
    strcpy(command[0], "AthomZip");
    strcpy(command[1], "-c");
    strcpy(command[2], "myzip.zip");
    strcpy(command[3], "/my folder/");
    strcpy(command[4], "/mercurial.exe");
    strcpy(command[5], "/java.pdf");

    Reply resp = ConsoleManager::executeCommand(commandSize, command);
    CHECK_EQUAL("Compress", resp.message_);
    CHECK_EQUAL(0, resp.code_);
    freeCommand(command, commandSize);
}
